M291 R"Wait for Extruders to reach required temperature" P" " S0

T1 P0
T0 P0
G10 P0 R270 S270
G10 P1 R270 S270
M116 P0 S10
M116 P1 S10

G28
G1 F18000 X-50 U50 Y-150 Z200
M291 R"Clean both Nozzles well with a metal brush" P"Otherwise you risk damaging your printer. Click OK when ready to proceed" S3


G1 Y160
M291 R"Connect the probe and place it in the center of the print bed" P"Click OK when ready to proceed" S3
; Park second tool
T0
G90 ; Absolute
G0 U999 F18000
G0 X30 Y30 Z23 ; Move main tool to position

M291 R"Slide the probe under the nozzle" P"Click OK when ready to proceed" S3
M42 P4 S1 ; Turn on relay
G4 P500

G91 ; Relative
G38.2 K1 Z-3 ; Probe Z
G0 Z2

; ===== Probe with the Right tool =====

;Move to the 1st probing point
G0 Y0 X-20        ;Move to position
G0 Z-2.5
G38.2 K1 X30 ; Probe LX
var spLX = move.axes[0].userPosition

;Move to the 2nd probing point
G0 X-2
G0 Y15
G0 X30
G0 Y-15
G38.2 K1 X-30 ; Probe LX
var fpLX = move.axes[0].userPosition

;Move to the 3rd probing point
G0 X2
G0 Y15
var diffLX = var.fpLX - var.spLX
var cpLX = var.fpLX - var.diffLX / 2
G90 ; Absolute
G0 X{var.cpLX}
G91 ; Back to relative
G38.2 K1 Y-30 ; Probe LY
var fpLY = move.axes[1].userPosition

;Move to the 4th probing point
G0 Y2
G0 X-15
G0 Y-30
G90 ; Absolute
G0 X{var.cpLX}
G91 ; Back to relative
G38.2 K1 Y30 ; Probe LY
var spLY = move.axes[1].userPosition

var diffLY = var.fpLY - var.spLY
var cpLY = var.fpLY - var.diffLY / 2

G0 Y-2
G0 X-999


; ===== Probe with the Left tool =====


;Move to the 1st probing point
G90 ; Absolute
G0 U{var.cpLX + 20} Y{var.cpLY}
G91 ; Back to relative
G38.2 K1 U-30 ; Probe RX (first)
var fpRX = move.axes[3].userPosition


;Move to the 2nd probing point
G0 U2
G0 Y15
G0 U-30
G0 Y-15
G38.2 K1 U30 ; Probe RX
var spRX = move.axes[3].userPosition

var diffRX = var.fpRX - var.spRX
var cpRX = var.fpRX - var.diffRX / 2

;Move to the 3rd probing point
G0 U-2
G0 Y15
G90 ; Absolute
G0 U{var.cpRX}
G91 ; Back to relative
G38.2 K1 Y-30 ; Probe RY
var fpRY = move.axes[1].userPosition

;Move to the 4th probing point
G0 Y2
G0 U15
G0 Y-30
G90 ; Absolute
G0 U{var.cpRX}
G91 ; Back to relative
G38.2 K1 Y30 ; Probe RY (second)
var spRY = move.axes[1].userPosition
var diffRY = var.fpRY - var.spRY
var cpRY = var.fpRY - var.diffRY / 2

M42  P4 S0

set global.uoffset = var.cpLX - var.cpRX
set global.yoffset = var.cpLY - var.cpRY

; Generate uoffset.g (persistent uoffset var)
echo >"0:/user/uoffset.g" "; THIS FILE IS AUTOGENERATED !"
echo >>"0:/user/uoffset.g" "; Please edit contents of '/macros/Calibration/Offsets/XY Tool Alignment'"
echo >>"0:/user/uoffset.g" "if exists(global.uoffset)"
echo >>"0:/user/uoffset.g" "  set global.uoffset = "^{global.uoffset}
echo >>"0:/user/uoffset.g" "else"
echo >>"0:/user/uoffset.g" "  global uoffset = "^{global.uoffset}
; Generate yoffset.g (persistent yoffset var)
echo >"0:/user/yoffset.g" "; THIS FILE IS AUTOGENERATED !"
echo >>"0:/user/yoffset.g" "; Please edit contents of '/macros/Calibration/Offsets/XY Tool Alignment'"
echo >>"0:/user/yoffset.g" "if exists(global.yoffset)"
echo >>"0:/user/yoffset.g" "  set global.yoffset = "^{global.yoffset}
echo >>"0:/user/yoffset.g" "else"
echo >>"0:/user/yoffset.g" "  global yoffset = "^{global.yoffset}
; Generate tooloffset.g
echo >"0:/user/tooloffset.g" "; THIS FILE IS AUTOGENERATED !"
echo >>"0:/user/tooloffset.g" "; Please edit contents of files in '/macros/Calibration/XY Tool Alignment'"
echo >>"0:/user/tooloffset.g" "; Load persistent variables"
echo >>"0:/user/tooloffset.g" ""
echo >>"0:/user/tooloffset.g" "; Set tool offsets"
echo >>"0:/user/tooloffset.g" "G10 P1 U"^{global.uoffset}^" Y"^{global.yoffset}

; Apply changes
G10 P1 U{global.uoffset} Y{global.yoffset}
echo "Current offsets: U"^{global.uoffset}^" Y"^{global.yoffset}

; Park second tool
G0 Z5
G90
G0 X-999 U999 Y150 Z200 F18000

G10 P0 R0 S0
G10 P1 R0 S0

M291 R"Finished successfully" P"You can now remove the probe" T10